package pl.jtb.ctl.tabory.ui.boe.ui.ve;

import java.util.Date;

import org.apache.log4j.Logger;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import pl.jtb.ctl.tabory.dao.data.vo.CompositionVO;
import pl.jtb.ctl.tabory.dao.data.vo.VagonVO;
import pl.jtb.ctl.tabory.ui.boe.osgi.Activator;
import pl.jtb.ctl.tabory.ui.boe.selection.VagonSelection;
import pl.jtb.ctl.tabory.ui.editors.AbstractDetailSectionPart;
import pl.jtb.ctl.tabory.ui.selection.ISelectionListener;
import pl.jtb.ctl.tabory.ui.util.CommonDateUtils;
import pl.jtb.ctl.tabory.ui.util.CommonValidationUtil;

public class VagonDetailSectionPart extends AbstractDetailSectionPart<VagonSelection> {

    private final static Logger logger = Logger.getLogger(VagonDetailSectionPart.class);

    private Composite client;

    private Text fieldVagonName;
    private DateTime revisionOneFromControl;
    private DateTime revisionOneUntilControl;
    private DateTime revisionTwoFromControl;
    private DateTime revisionTwoUntilControl;
    private Text fieldCompositionName;
    private Text fieldRevisionDistance;
    private Text fieldRevisionCorrection;
    private Text filedDistance;

    public VagonDetailSectionPart(Composite parent, FormToolkit toolkit, int style, String sectionName) {
        super(parent, toolkit, style, sectionName);
    }

    @Override
    public VagonSelection getSelection() {
        logger.debug("Retriving selection.");
        try {
            VagonVO selectedVagon = selection.getSelectedVagon();
            if (selectedVagon == null) {
                selectedVagon = new VagonVO();
            }
            selectedVagon.setInternalId(fieldVagonName.getText());
            selectedVagon.setRevisionCorrection(Double.parseDouble(fieldRevisionCorrection.getText()));
            selectedVagon.setRevisionDistance(Double.parseDouble(fieldRevisionDistance.getText()));
            Date dateRevOneFrom = CommonDateUtils.getDate(revisionOneFromControl.getYear(), revisionOneFromControl.getMonth(), revisionOneFromControl.getDay());
            Date dateRevOneUntil = CommonDateUtils.getDate(revisionOneUntilControl.getYear(), revisionOneUntilControl.getMonth(), revisionOneUntilControl.getDay());
            Date dateRevTwoFrom = CommonDateUtils.getDate(revisionTwoFromControl.getYear(), revisionTwoFromControl.getMonth(), revisionTwoFromControl.getDay());
            Date dateRevTwoUntil = CommonDateUtils.getDate(revisionTwoUntilControl.getYear(), revisionTwoUntilControl.getMonth(), revisionTwoUntilControl.getDay());
            selectedVagon.setRevOneFrom(dateRevOneFrom);
            selectedVagon.setRevOneUntil(dateRevOneUntil);
            selectedVagon.setRevTwoFrom(dateRevTwoFrom);
            selectedVagon.setRevTwoUntil(dateRevTwoUntil);
            selection.setSelectedVagon(selectedVagon);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return selection;
    }

    @Override
    public void setSelection(VagonSelection selection) {
        this.selection = selection;
        postInitGui(null, null);
        switch (selection.getEditorState()) {
            case NEW:
                fieldVagonName.setText("");
                filedDistance.setText("");
                fieldCompositionName.setText("");
                fieldRevisionCorrection.setText("0.0");
                fieldRevisionDistance.setText("0.0");
                client.setVisible(true);
                setEnabled(true);
                fieldCompositionName.setEnabled(false);
                fieldVagonName.setFocus();
                break;
            case CLEAN:
                fillEditorValues();
                setEnabled(false);
                break;
            case EDITING:
                fillEditorValues();
                setEnabled(true);
                fieldCompositionName.setEnabled(false);
                fieldVagonName.setFocus();
                break;
        }
    }

    private void fillEditorValues() {
        VagonVO selectedVagon = selection.getSelectedVagon();
        if (selectedVagon != null) {
            client.setVisible(true);
            fieldVagonName.setText(selectedVagon.getInternalId());
            filedDistance.setText(String.valueOf(selectedVagon.getDistance()));
            fieldRevisionCorrection.setText(String.valueOf(selectedVagon.getRevisionCorrection()));
            fieldRevisionDistance.setText(String.valueOf(selectedVagon.getRevisionDistance()));
            CompositionVO compositionForVagon = selection.getCompositionForVagon(selectedVagon);
            fieldCompositionName.setText(compositionForVagon != null ? compositionForVagon.getInternalId() : "---");
            Date dateRevOneFrom = selectedVagon.getRevOneFrom();
            Date dateRevOneUntil = selectedVagon.getRevOneUntil();
            Date dateRevTwoFrom = selectedVagon.getRevTwoFrom();
            Date dateRevTwoUntil = selectedVagon.getRevTwoUntil();
            revisionOneFromControl.setDate(CommonDateUtils.getYear(dateRevOneFrom), CommonDateUtils.getMonth(dateRevOneFrom), CommonDateUtils.getDay(dateRevOneFrom));
            revisionOneUntilControl.setDate(CommonDateUtils.getYear(dateRevOneUntil), CommonDateUtils.getMonth(dateRevOneUntil), CommonDateUtils.getDay(dateRevOneUntil));
            revisionTwoFromControl.setDate(CommonDateUtils.getYear(dateRevTwoFrom), CommonDateUtils.getMonth(dateRevTwoFrom), CommonDateUtils.getDay(dateRevTwoFrom));
            revisionTwoUntilControl.setDate(CommonDateUtils.getYear(dateRevTwoUntil), CommonDateUtils.getMonth(dateRevTwoUntil), CommonDateUtils.getDay(dateRevTwoUntil));
        } else {
            client.setVisible(false);
        }
    }

    @Override
    public void addSelectionListener(ISelectionListener<VagonSelection> listner) {
    }

    @Override
    public void removeSelectionListener(ISelectionListener<VagonSelection> listner) {
    }

    @Override
    protected void initGui(Section parentSection, FormToolkit toolkit) {
        logger.debug("Initializing GUI.");

        GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parentSection);
        client = toolkit.createComposite(parentSection);
        GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(client);
        parentSection.setClient(client);

        Label labelVagonName = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.vagonname"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelVagonName);
        fieldVagonName = toolkit.createText(client, null);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(120, SWT.DEFAULT).applyTo(fieldVagonName);

        Label labelCompositionName = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.compositioninternalid"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelCompositionName);
        fieldCompositionName = toolkit.createText(client, null);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(120, SWT.DEFAULT).applyTo(fieldCompositionName);

        Label labelRevisionOneFrom = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.revisiononefrom"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelRevisionOneFrom);
        revisionOneFromControl = new DateTime(client, SWT.DATE | SWT.DROP_DOWN | SWT.BORDER);
        toolkit.adapt(revisionOneFromControl);
        toolkit.paintBordersFor(revisionOneFromControl);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(80, SWT.DEFAULT).applyTo(revisionOneFromControl);

        Label labelRevisionOneUntil = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.revisiononeuntil"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelRevisionOneUntil);
        revisionOneUntilControl = new DateTime(client, SWT.DATE | SWT.DROP_DOWN | SWT.BORDER);
        toolkit.adapt(revisionOneUntilControl);
        toolkit.paintBordersFor(revisionOneUntilControl);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(80, SWT.DEFAULT).applyTo(revisionOneUntilControl);

        Label labelRevisionTwoFrom = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.revisiontwofrom"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelRevisionTwoFrom);
        revisionTwoFromControl = new DateTime(client, SWT.DATE | SWT.DROP_DOWN | SWT.BORDER);
        toolkit.adapt(revisionTwoFromControl);
        toolkit.paintBordersFor(revisionTwoFromControl);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(80, SWT.DEFAULT).applyTo(revisionTwoFromControl);

        Label labelRevisionTwoUntil = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.revisiontwountil"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelRevisionTwoUntil);
        revisionTwoUntilControl = new DateTime(client, SWT.DATE | SWT.DROP_DOWN | SWT.BORDER);
        toolkit.adapt(revisionTwoUntilControl);
        toolkit.paintBordersFor(revisionTwoUntilControl);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(80, SWT.DEFAULT).applyTo(revisionTwoUntilControl);

        Label labelRevisionDistance = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.revisiondistance"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelRevisionDistance);
        fieldRevisionDistance = toolkit.createText(client, null);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(120, SWT.DEFAULT).applyTo(fieldRevisionDistance);

        Label labelRevisionCorrection = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.revisioncorrection"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelRevisionCorrection);
        fieldRevisionCorrection = toolkit.createText(client, null);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(120, SWT.DEFAULT).applyTo(fieldRevisionCorrection);

        Label labelDistance = toolkit.createLabel(client, Activator.getMessage("vagons.editor.label.vagon.distance"));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(labelDistance);
        filedDistance = toolkit.createText(client, null);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, false).hint(120, SWT.DEFAULT).applyTo(filedDistance);

        toolkit.adapt(client);
        toolkit.paintBordersFor(client);
        client.setVisible(false);
    }

    @Override
    protected void postInitGui(Section parentSection, FormToolkit toolkit) {
        if (selection == null) {
            return;
        }
        fieldRevisionCorrection.addKeyListener(CommonValidationUtil.getValidationDoubleKeyAdapter());
        fieldRevisionDistance.addKeyListener(CommonValidationUtil.getValidationDoubleKeyAdapter());
    }

    @Override
    protected void initActions() {
    }

    @Override
    public void setEnabled(boolean isEnabled) {
        fieldVagonName.setEnabled(isEnabled);
        revisionOneFromControl.setEnabled(isEnabled);
        revisionOneUntilControl.setEnabled(isEnabled);
        revisionTwoFromControl.setEnabled(isEnabled);
        revisionTwoUntilControl.setEnabled(isEnabled);
        fieldRevisionDistance.setEnabled(isEnabled);
        fieldCompositionName.setEnabled(isEnabled);
        fieldRevisionCorrection.setEnabled(isEnabled);
        filedDistance.setEnabled(false);
    }

}
